layui.config({
    base : "/layui/"
}).extend({
    index: 'lib/index'
}).use(['index','form','jquery', 'table'],function(){
    var form = layui.form,
        table=layui.table,
        admin  = layui.admin;
    table.render({
        elem: '#enterprise_Terminal_table'
        ,url: '/enterprise_terminal/queryData'
        ,method:"post"
        ,height: 500
        ,limit:50							//显示数目
        ,limits:[50,100]
        ,cols: [[
            {field:'id', title: 'ID',width:100, "align":"center"}
            ,{field:'deviceId', title: '设备编码(自动生成)',width:200, "align":"center"}
            ,{field:'imei',width:200, title: 'IMEI',"align":"center"}
            ,{field:'type',width:100, title: '设备类型',"align":"center",templet:function (d) {
                    if (d.type=="ALIPAY"){
                        return "支付宝";
                    }else if (d.type=="WECHAT"){
                        return  "微信";
                    }else if (d.type=="QRCODE"){
                        return  "二维码";
                    }else{
                        return "";
                    }

                }}
            ,{field:'sn',width:200, title: '设备序号',"align":"center"}
            ,{field:'storeId',width:200, title: '所属门店ID',"align":"center"}
            ,{field:'storeName',width:200, title: '所属门店名',"align":"center"}
            ,{field:'createdTime',width:200, title: '创建时间',"align":"center"}
            ,{field:'OPT', width:300, title: '操作',"align":"center",templet:function (d) {
                       return '<a  href="#" class="layui-btn layui-btn-xs" lay-event="bing_store" >门店绑定</a>' +
                           '<a  href="#" class="layui-btn layui-btn-xs" lay-event="unlock_store" >解锁</a>';
                }}
        ]]
        ,request:{
            pageName: 'pageNo' //页码的参数名称，默认：page
            ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
        },
        where:{}
        ,id: 'enterprise_Terminal_table'
        ,page: true
    });


    table.on('tool(enterprise_Terminal_table)', function(obj){
        var table_event = obj.event;

        var data = obj.data;

        if(table_event=="unlock_store"){
            layer.msg('此操作将清除设备imei，设备将解锁', {
                time: 0, //20s后自动关闭
                id: 'LAY_layuipro',
                shade: 0.8,
                btnAlign: 'c',
                btn: ['确定', '取消'],
                yes:function () {

                    var terminalId = data.id;
                    $.ajax({
                        url:"/agency_terminal/unLock",
                        data:{id:terminalId},
                        type:"post",
                        success:function (result) {
                            if (result.code == 1){
                                layer.msg("解锁成功",{
                                    offset: '32%'
                                    ,icon: 1
                                    ,time: 0
                                    ,btn: '知道了'
                                    ,btnAlign:"c"
                                    ,shade:0.4
                                    ,yes:function () {
                                        table.reload('enterprise_Terminal_table', {
                                            page: {
                                                curr: 1 //重新从第 1 页开始
                                            }
                                            ,where: {

                                            }
                                        });
                                        layer.closeAll();
                                    }
                                })
                            }else{
                                layer.msg(result.message,{
                                    offset: '32%'
                                    ,icon: 2
                                    ,time: 3000
                                },function () {
                                    layer.closeAll();
                                })
                            }

                        }
                    });

                }
            });

        }else if(table_event=='bing_store'){

            initStoreSelect();

            setTimeout(function () {
              admin.popup({
                  title: '编辑'
                  ,shade: 0
                  ,anim: -1
                  ,offset:"20px"
                  ,area: ['80%', '500px']
                  ,skin: 'layui-anim layui-anim-upbit'
                  ,content:$("#bing_store_win")
                  ,end:function () {
                      table.reload('enterprise_Terminal_table', {
                          page: {
                              curr: 1 //重新从第 1 页开始
                          }
                          ,where: {

                          }
                      });
                  }
              });

              form.val('bing_store_form', {
                  "id":data.id,
                  "storeId":data.storeId
              })
          },200);
        }

    });

    //添加设备
    $(".enterpriseTerminalAdd_btn").click(function(){

        $("#add_enterpriseTerminal_form")[0].reset();
        /**
         * 初始化 等级下拉
         */

        admin.popup({
            title: '编辑'
            ,shade: 0
            ,anim: -1
            ,offset:"20px"
            ,area: ['80%', '500px']
            ,skin: 'layui-anim layui-anim-upbit'
            ,content:$("#add_enterpriseTerminal_win")
            ,end:function () {
                table.reload('enterprise_Terminal_table', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {

                    }
                });
            }
        });
        form.val('add_enterpriseTerminal_form', {
            "id":""
        });

    });


    form.on('submit(addenterpriseTerminalOk)',function (data) {

        $.ajax({
            url:"/enterprise_terminal/edit",
            data:data.field,
            type:"post",
            success:function (result) {
                if (result.code == 1){
                    layer.msg("编辑成功",{
                        offset: '32%'
                        ,icon: 1
                        ,time: 0
                        ,btn: '知道了'
                        ,btnAlign:"c"
                        ,shade:0.4
                        ,yes:function () {
                            table.reload('enterprise_Terminal_table', {
                            });
                            layer.closeAll();
                        }
                    })
                }else{
                    layer.msg(result.message,{
                        offset: '32%'
                        ,icon: 2
                        ,time: 3000
                    },function () {
                        layer.closeAll();
                    })
                }

            }
        });
        return false;
    });

    form.on('submit(bingStotreOk)',function (data) {

        $.ajax({
            url:"/enterprise_terminal/storeBing",
            data:data.field,
            type:"post",
            success:function (result) {
                if (result.code == 1){
                    layer.msg("绑定成功",{
                        offset: '32%'
                        ,icon: 1
                        ,time: 0
                        ,btn: '知道了'
                        ,btnAlign:"c"
                        ,shade:0.4
                        ,yes:function () {
                            table.reload('enterprise_Terminal_table', {
                            });
                            layer.closeAll();
                        }
                    })
                }else{
                    layer.msg(result.message,{
                        offset: '32%'
                        ,icon: 2
                        ,time: 3000
                    },function () {
                        layer.closeAll();
                    })
                }

            }
        });
        return false;
    });

    $(".search_btn").click(function () {
        table.reload('enterprise_Terminal_table', {
            where:{
                storeName:$(".queryTable input[name='storeName']").val()
            }
        });
    });


});


/**
 * 初始化门店下拉列表
 */
function initStoreSelect() {
    $.post(
        "/enterprise_store/getStores",
        {},
        function (result) {

            if(result.code==1){

                $('#bing_store_form select[name="storeId"] option').remove();
                $.each(result.data, function (index, item) {
                 var s =   $('#bing_store_form select[name="storeId"]');
                   s.append(new Option(item.name, item.id));// 下拉菜单里添加元素
                });

            }else {
                layer.msg(result.message,{
                    offset: '32%'
                    ,icon: 2
                    ,time: 3000
                },function () {
                    layer.closeAll();
                })
            }
        }
    );
};


